clear; clc;
load('data/recsys/movielen1m.mat');

para.tau = 1.01;
[row, col, val] = find(data);

[m, n] = size(data);

clear user item;

val = val - mean(val);
val = val/std(val);

idx = randperm(length(val));

traIdx = idx(1:floor(length(val)*0.5));
tstIdx = idx(ceil(length(val)*0.5): end);

clear idx;

traData = sparse(row(traIdx), col(traIdx), val(traIdx));
traData(size(data,1), size(data,2)) = 0;

% para.test.row  = row(tstIdx);
% para.test.col  = col(tstIdx);
% para.test.data = val(tstIdx);
% para.test.m = m;
% para.test.n = n;

clear m n;

%% start testing
para.maxIter = 5000;
para.decay = 0.8;
para.tol = 1e-1;

% %% nuclear norm
% lambda = 23;
% % APG
% method = 1;
% t = tic;
% [U, S, V, out{method,1}] = APGMatComp( traData, lambda, para );
% Time(method,1) = toc(t);
% RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% % SoftImpute
% method = 2;
% t = tic;
% para.speedup = 1;
% para.exact = 1;
% [ U, S, V, out{method,1} ] = SoftImpute( traData', lambda, para);
% Time(method,1) = toc(t);
% RMSE(method,1) = MatCompRMSE(V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% % ActiveSubspace
% method = 3;
% t = tic;
% % [ U, S, V, out{3} ] = ActiveSubspace( traData', lambda, para);
% [ U, S, V, out{method,1} ] = mc_alt( traData, lambda, para );
% Time(method,1) = toc(t);
% RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% % FastMatComp
% t = tic;
% method = 4;
% para.maxR = 200;
% para.speedup = 1;
% para.regType = 1;
% [U, S, V, out{method,1}] = FastMatComp( traData, lambda, 1e+8, para );
% Time(method,1) = toc(t);
% RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% clear U S V t;

%% non-convex
para.tol = 1;
for i = 3:5
    
para.decay = 0.95;   
para.regType = i - 2;
para.maxR = 12;

switch(para.regType)
    case 1
        lambda = 35;
%         lambda = 40;
        theta1 = lambda*2;
    case 2
        lambda = 2000;
        % lambda = 5000;
        theta1 = sqrt(lambda);
    case 3
        lambda = 35;
%         lambda = 40;
        theta1 = 3;
end

% % FAST
% method = 1;
% t = tic;
% para.speedup = 0;
% % [U, S, V, out{i,method} ] = IRNNMatCompSL( traData, lambda, theta1, para );
% % [U, S, V, out{i,method} ] = GISTMatCompSL( traData, lambda, theta1, para );
% [U, S, V, out{method,i}] = FastMatComp( traData, lambda, theta1, para );
% Time(method,i) = toc(t);
% RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

method = 2;
t = tic;
para.speedup = 1;
[U, S, V, out{method,i}] = FastMatComp( traData, lambda, theta1, para );
Time(method,i) = toc(t);
RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% GIST
method = 3;
t = tic;
para.acc = 0;
[U, S, V, out{method,i}] = GISTMatComp( traData, lambda, theta1, para);
Time(method,i) = toc(t);
RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% % IRNN
% method = 4;
% t = tic;
% [U, S, V, out{method,i}] = IRNNMatComp( traData, lambda, theta1, para);
% Time(method,i) = toc(t);
% RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% clear U S V t method estRank;

end

%% Fixed Rank
estRank = 7;
method = 1;
t = tic;
[U, S, V, out{method,2} ] = SoftImputeALS( traData', 1e-7, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));

estRank = 20;
method = 2;
t = tic;
[U, S, V, out{method,2} ] = EOR1MP( traData, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

clear U S V method t estRank movies Age;
clear i col row theta1 traData traIdx tstIdx user val data item para lambda;

save('ml1m-rst3')



